
import Head from 'next/head'

<Head>
  <script>
    {
      `(function() {
         var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?e60fb290e204e04c5cb6f79b0ac1e697";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
       })();`
    }
  </script>
</Head>

![LangChain](https://pica.zhimg.com/50/v2-56e8bbb52aa271012541c1fe1ceb11a2_r.gif)





Pinecone [#](#pinecone "此标题的永久链接")
===========================

[松果（Pinecone）](https://docs.pinecone.io/docs/overview)是一个功能广泛的向量数据库。

本教程展示了如何使用与`松果（Pinecone）`向量数据库相关的功能。

要使用松果（Pinecone），您必须拥有API密钥。以下是[安装说明](https://docs.pinecone.io/docs/quickstart)。

```python
!pip install pinecone-client

```

```python
import os
import getpass

PINECONE_API_KEY = getpass.getpass('Pinecone API Key:')

```

```python
PINECONE_ENV = getpass.getpass('Pinecone Environment:')

```

我们想使用`OpenAI Embeddings`，因此我们必须获取OpenAI API密钥。

```python
os.environ['OPENAI_API_KEY'] = getpass.getpass('OpenAI API Key:')

```

```python
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import Pinecone
from langchain.document_loaders import TextLoader

```

```python
from langchain.document_loaders import TextLoader
loader = TextLoader('../../../state_of_the_union.txt')
documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs = text_splitter.split_documents(documents)

embeddings = OpenAIEmbeddings()

```

```python
import pinecone 

# initialize pinecone
pinecone.init(
    api_key=PINECONE_API_KEY,  # find at app.pinecone.io
    environment=PINECONE_ENV  # next to api key in console
)

index_name = "langchain-demo"

docsearch = Pinecone.from_documents(docs, embeddings, index_name=index_name)

# if you already have an index, you can load it like this
# docsearch = Pinecone.from_existing_index(index_name, embeddings)

query = "What did the president say about Ketanji Brown Jackson"
docs = docsearch.similarity_search(query)

```

```python
print(docs[0].page_content)

```

